package com.chezhujie.www.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjdataresource;
import com.chezhujie.www.domain.Czjtemp;
import com.chezhujie.www.persistence.CzjtempMapper;

/**
 * Created by 汤伟杰 on 2017/6/2.
 */
@Service
public class CzjtempService {
   
	private static final String code = "SORT.CXSX";
	
	@Autowired
	private  CzjtempMapper czjtempMapper;
	
	 @Autowired
	    private JdbcTemplate jdbcTemplate;

	public List<Map<String, Object>> listCzjtemp() {
		 List<Map<String, Object>> listmap = jdbcTemplate.queryForList("select * from czj_xy_temp where code='" + code + "' order by value asc");
		  
	        return listmap;
	}

	public Pagination list() {
		   Pagination pagination = new Pagination();
	        pagination.setPageNo(1);
	        pagination.setPageSize(1000);
	        List<Map<String, Object>> listmap = jdbcTemplate.queryForList("select * from czj_xy_temp where code='" + code + "' order by value asc");
	        pagination.setList(listmap);
	        pagination.setTotalCount(listmap.size());
	        return pagination;
	}

	public void exchange(String id, Integer sorting) throws Exception {
		
		 Integer location=null;
	        Integer no = null;
	        Czjdataresource czjdataresource = (Czjdataresource) jdbcTemplate.queryForObject("select * from czj_xy_dataresource where dataresourceId='" + id + "' order by createtime desc limit 1", new BeanPropertyRowMapper(Czjdataresource.class));
	        try {
	            //位置顺序
	            no = Integer.parseInt(String.valueOf(czjdataresource.getValue()));
	            location = no+sorting;
	        } catch (Exception e) {
	            e.printStackTrace();
	            throw new Exception("切换顺序失败！顺序值只能是数字，但出现 "+czjdataresource.getValue());
	        }
	        //查询已存在的进行修改
	        String update1 = "update czj_xy_dataresource set value='"+no+"' where code='" + code + "' and value='" + location + "'";
	        jdbcTemplate.update(update1);
	        //更新id交换的数据
	        String update2 = "update czj_xy_dataresource set value='"+location+"' where dataresourceId='"+id+"'";
	        jdbcTemplate.update(update2);
		
	}

	public void deleteById(String dataresourceId, Integer op) throws Exception{
		
			if(op==1){
				op=0;
			}else{
				op=1;
			}
			
		  String update= "update czj_xy_temp set op='"+op+"' where dataresourceId='"+dataresourceId+"'";
		   jdbcTemplate.update(update);
		   
	}

	public Czjtemp getTempById(Map<String, Object> map) {
		return czjtempMapper.getTempById(map);
	}

	public void updateTemp(Czjtemp czjtemp) {
		czjtempMapper.updateTemp(czjtemp);
	}

	public void exchange1(String id, Integer sorting) throws Exception {

		 Integer location=null;
	        Integer no = null;
	        Czjdataresource czjdataresource = (Czjdataresource) jdbcTemplate.queryForObject("select * from czj_xy_temp where dataresourceId='" + id + "' order by createtime desc limit 1", new BeanPropertyRowMapper(Czjdataresource.class));
	        try {
	            //位置顺序
	            no = Integer.parseInt(String.valueOf(czjdataresource.getValue()));
	            location = no+sorting;
	        } catch (Exception e) {
	            e.printStackTrace();
	            throw new Exception("切换顺序失败！顺序值只能是数字，但出现 "+czjdataresource.getValue());
	        }
	        //查询已存在的进行修改
	        String update1 = "update czj_xy_temp set value='"+no+"' where code='" + code + "' and value='" + location + "'";
	        jdbcTemplate.update(update1);
	        //更新id交换的数据
	        String update2 = "update czj_xy_temp set value='"+location+"' where dataresourceId='"+id+"'";
	        jdbcTemplate.update(update2);
	}
	
}
